import os, sha, getpass, sys
from Ft.Server.Server import GlobalConfig

def GetConfig(conffile=None,ftss_agent=1):
    
    if ftss_agent:
        agent = os.environ.get('FTSS_AGENT')
        if agent:
            exec('agent=%s' % agent)
            username, password, host, port = agent
        else:
            ftss_agent = 0
            
    if not ftss_agent:
        username = raw_input("4SS Manager Name: ")
        password = getpass.getpass("Password for %s: " % username)
        password = sha.new(password).hexdigest()
    
    core = os.environ.get('FTSS_CORE_ID', 'Core')
    debug = 1
    
    if not conffile:
        conffile = os.environ.get('FTSS_SERVER_CONFIG','4ss.conf')
    
    config = GlobalConfig.GlobalConfig(username, password, core,
                                       conffile, debug)
    
    config.readConfig()
    config.openLogs()
    
    return config

def GetRepo(conffile=None,ftss_agent=1):
    return GetConfig(conffile,ftss_agent).getRepository()

def GetModel(conffile=None,ftss_agent=1):
    return GetRepo(conffile,ftss_agent).getModel()